#/** @file
#
#  Copyright (C) 2022, Phytium Technology Co., Ltd. All rights reserved.<BR>
#
#  SPDX-License-Identifier: BSD-2-Clause-Patent
#
#**/
#include <AsmMacroIoLibV8.h>
#include <AutoGen.h>
#include <Base.h>
#include <Library/ArmLib.h>
#include <Library/PcdLib.h>


.text
.align 2

GCC_ASM_EXPORT(ArmPlatformPeiBootAction)
GCC_ASM_EXPORT(ArmPlatformIsPrimaryCore)
GCC_ASM_EXPORT(ArmPlatformGetPrimaryCoreMpId)
GCC_ASM_EXPORT(ArmPlatformGetCorePosition)

PrimaryCoreMpid:  .word    0x0


ASM_PFX(ArmPlatformPeiBootAction):
  mov   x20, x30
  bl    ASM_PFX(ArmReadMpidr)
  lsl   w0, w0, #8
  lsr   w0, w0, #8
  ldr   x1, =PrimaryCoreMpid
  str   w0, [x1]

  mrs x0, sctlr_el2
  ldr x1, =(0x1 << 12)
  orr x0, x0, x1
  msr sctlr_el2, x0
  isb
  ret   x20


ASM_PFX(ArmPlatformGetPrimaryCoreMpId):
  ldr   x0, =PrimaryCoreMpid
  ldr   w0, [x0]
  ret

ASM_PFX(ArmPlatformIsPrimaryCore):
  mov   x20, x30
  bl    ASM_PFX(ArmReadMpidr)
  lsl   w0, w0, #8
  lsr   w0, w0, #8
  MOV32 (w1, FixedPcdGet32 (PcdArmPrimaryCore))
  cmp   w0, w1
  cset  x0, eq
  ret   x20


ASM_PFX(ArmPlatformGetCorePosition):
  and   x1, x0, #ARM_CORE_MASK
  and   x0, x0, #ARM_CLUSTER_MASK
  add   x0, x1, x0, LSR #6
  ret

ASM_FUNCTION_REMOVE_IF_UNREFERENCED
